import okhttp3.*;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class AliyunDemo {
    public static void main(String[] args) throws IOException {
        String host = "http://simpleocr.market.alicloudapi.com";
        String path = "/ocr/onlyText";
        String method = "POST";
        String appcode = "填上你的APPCODE";
//        Map<String, String> headers = new HashMap<String, String>();
//        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
//        headers.put("Authorization", "APPCODE " + appcode);
//        //根据API的要求，定义相对应的Content-Type
//        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();


        MediaType mediaType = MediaType.parse("text/plain");
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("multipartFile","dingdan.jpg",
                        RequestBody.create(MediaType.parse("application/octet-stream"),
                                new File("dingdan.jpg")))
                .build();

        System.out.println(host+path);
        Request request = new Request.Builder()
                .url(host+path)
                .method("POST", body)
                .addHeader("Authorization", "APPCODE " + appcode)
                .build();

        Response response = client.newCall(request).execute();
        //get response json text
        String result = response.body().string();
        System.out.println(result);
    }

}
